<?php
class VersionAction extends Action {
	public function index() {
		import('ORG.Util.Page');
		$product=M('Product');

		$count=$product->join(C('DB_PREFIX').'version on '.C('DB_PREFIX').'product.id='.C('DB_PREFIX').'version.product_id ')->where(C('DB_PREFIX').'version.version is not null ')->count();

		$Page = new Page($count, C('PAGE_NUM'));

		$list=$product->join(C('DB_PREFIX').'version on '.C('DB_PREFIX').'product.id='.C('DB_PREFIX').'version.product_id ')->where(C('DB_PREFIX').'version.version is not null ')->limit($Page->firstRow. ',' . $Page->listRows)->order(C('DB_PREFIX').'version.id desc')->select();
		
		$Page->setConfig('header', '条数据');
        $Page->setConfig('first', '<<');
        $Page->setConfig('last', '>>');
        $page = $Page->show();
		$this->assign("count",$count);
        $this->assign("page", $page);
        $this->assign("list", $list);
		$this->display();
		
	}
	
	public function showadd(){
		$product=M('Product');
		$list=$product->select();
		$this->assign("list",$list);
		$this->display();
	}

	public function add(){
		$version=M('Version');
		$list=$version->where(array('product_id'=>$_POST['product_id'],'version'=>$_POST['version']))->select();
		$count=count($list);
		if($count>0){
			$this->error('添加错误,已经存在此版本');
		}else{
			$result=$version->add($_POST);
			if($result){
				$this->success('添加成功');
				$this->redirect('Version/index');
			}else{
				$this->error('添加错误');
			}
		}
	}
	
	public function getVersion(){
		$product_id=$_GET['product_id'];
		$version=M('Version');
		$list=$version->where(array('product_id'=>$product_id))->field('version')->select();
		echo "<option value=''>--请选择产品版本号--</option>";
		foreach($list as $k=>$v){
			echo '<option value='.$v['version'].'>'.$v['version'].'</option>';
			
		}
	}
	public function edit(){
		$id=$_GET['id'];
		$version=M('Version');
		$list=$version->where(array('id'=>$id))->select();
		$vo=current($list);
		$product_id=$vo['product_id'];
		$plist=M('Product')->where(array('id'=>$product_id))->select();
		$pvo=current($plist);
		$vo['product_id']=$pvo['name'];
		$this->assign('vo',$vo);
		$this->display();
	}

	public function save(){
		$id=$_POST['id'];
		$version=M('Version');
		$list=$version->where(array('id'=>$id))->select();
		$vo=current($list);
		$product_id=$vo['product_id'];
		$data['version']=$_POST['version'];

		$r=$version->where(array('product_id'=>$product_id,'version'=>$data['version']))->select();
		if($r){
			$this->error('此产品已经存在此版本号');
		}else{
			$version->where(array('id'=>$id))->save($data);
			$this->redirect('Version/edit',array('id'=>$id),1,'编辑成功');
		}

	}
}
?>